<template>
	<view class="index-box" :style="{ '--color': vuex_config.themeColor1,'--price':vuex_config.price }"
		v-if="this.selectList.length == this.dataArr.length">
		<sww-wx-address></sww-wx-address>
		<pick-up-com ref="pick_up_view" @changPick="changPick"></pick-up-com>
		<view class="box">
			<!-- 规格信息-->
			<prod-list :selectList='selectList' />

			<order-cell :params='orederCell' />

			<template v-if="checkPick">
				<view class="cell_item">
					<view>收货人</view>
					<input placeholder="请输入收货人姓名" class="cell_center" type="text" v-model="pickAddress.name" />
					<!-- <view class="iconfont iconfont-e775"></view> -->
				</view>
				<view class="cell_item">
					<view>手机号</view>
					<input placeholder="请输入联系电话" class="cell_center" type="number" v-model="pickAddress.mobile" />
					<!-- <view class="iconfont iconfont-e775"></view> -->
				</view>
				<view class="cell_item">
					<view>备注</view>
					<input placeholder="请输入备注" class="cell_center" type="number" v-model="remark" />
					<!-- <view class="iconfont iconfont-e775"></view> -->
				</view>
			</template>
		</view>
		<!-- 支付方式 -->
		<view class="box">
			<sww-pay-type v-model="dataForm.pay_type" :price="totalPrice"></sww-pay-type>
		</view>
		<sww-goods-action>
			<view class="sub-price">
				<text class="">总计：</text>
				<text class="sww-tp">￥{{ $price(totalPrice) }}</text>
			</view>
			<view class="sub-btn" @click="onSubmit">提交订单</view>
		</sww-goods-action>
		<sww-login></sww-login>
	</view>
</template>
<script>

	import {
		localPostage
	} from '@/mixins/localPostage';
	import {
		payOrder
	} from '@/mixins/payOrder';
	import {
		confirm
	} from './orderConfirm/index.js';
	import prodList from './orderConfirm/prodList.vue';
	import orderCell from './orderConfirm/orderCell.vue'
	import pickUpCom from './pickupComponent/index.vue';
	import {
		isMobile
	} from '@/utils/validate.js';
	export default {
		mixins: [confirm, localPostage, payOrder],
		components: {
			prodList,
			orderCell,
			'pick-up-com': pickUpCom
		},
		data() {
			return {
				order_type: 1,
				proList: [],
				orederCell: {},
				remark: '',
				codeList:'',
				checkPick: false, // 是否选择了自提地址
				pickAddress: {
					name: '',
					mobile: '',

				},
			};
		},
		onShow() {
			this.$nextTick(() => {
				this.$refs.pick_up_view.$show();
			})
		},
		methods: {
			// 自提选择发生变化
			changPick(val) {
				this.checkPick = val;
			},
			
			createProductOrder(order_no){
				this.$resPost('AddCodeWriteOff', {
					code:this.codeList,
					order_no
				})
			},

			onAddOrderPay() {
				return new Promise((resolve, reject) => {
					//核销
					if(!this.codeList){
						reject()
						return
					}
					this.$resPost('AddCodeWriteOff', {
						code:this.codeList
					}).then(res => {
						if (res.Status == 1) {
							resolve()
							return
						}
						reject()
					}).catch(() => {
						reject()
					})
				})
			},

			// 提交表单
			onSubmit() {
				this.$login('onSubmit').then(res => {
					this.$u.debounce(
						() => {
							if (this.checkPick) {
								if (!this.pickAddress.name) {
									this.$toast('请输入联系人名称');
									return;
								} else if (!isMobile(this.pickAddress.mobile)) {
									this.$toast('请正确输入联系方式');
									return;
								}
							} else {
								if (!this.vuex_address) {
									this.$toast('请选择收货地址');
									return;
								}
							}
							if (!this.$checkCanBuyFromPostageModule()) return;
							let {
								totalPrice,
								dataList,
								dataArr
							} = this
							let dataJson = dataList
							let json = []


							this.dataArr.forEach(el => {
								json.push({
									fk_pro_id: el.fk_prodtct_id,
									num: el.Pronum,
									fk_stock_id: el.stock_id,
									price: 0,
									name: el.stock_name

								})
							})

							let remark = '卡号兑换下单，折扣了'
							remark += totalPrice > 0 ? `:￥${this.orederCell.price}元` :
								` : ￥${totalPrice + this.orederCell.price}元`

							if (this.remark) remark += `;核销号:${this.codeList};备注：${this.remark}`
							let params = {
								...this.vuex_address,
								remark,
								postage: totalPrice, // 邮费
								total_amount: totalPrice, // 订单总金额
								coupon_id: '0',
								coupon: '0',
								type: 1,
								json_detailed: JSON.stringify(json)
							};


							if (this.checkPick) {
								let pickForm = this.$refs.pick_up_view.pickForm;
								params = {
									...params,
									...this.pickAddress,
									address: `[自提]${pickForm.name} ${pickForm.address}`
								}
							}
							this._submitOrder(params, {
								totalPrice: params.total_amount,
								pay_type: this.dataForm.pay_type,
								order_type: this.order_type,
							})
						},
						1000,
						true
					);
				})

			}
		}
	};
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.index-box {
		width: 100%;

		.cell_item {
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			height: 60rpx;
			box-sizing: content-box;

			.cell_center {
				flex: 1;
				overflow: hidden;
				text-align: right;
				color: #999;
			}

			.iconfont {
				color: #666;
				margin-left: 8rpx;
				position: relative;
			}
		}

		.box {
			width: 100%;
			padding: 0 24rpx;
			background-color: #ffffff;
			margin-bottom: 10rpx;
		}

		.sub-price {
			padding-left: 30rpx;
			font-size: 36rpx;
			font-weight: bold;
		}

		.sub-btn {
			width: 250rpx;
			height: 100rpx;
			background: var(--color);
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ffffff;
			font-size: 30rpx;
			margin-left: auto;
		}
	}
</style>
